import 'package:azlistview/azlistview.dart';

import '../const_value.dart';
import '../utils.dart';

class Friend extends ISuspensionBean {

  static const tableName = "im_friend";

  static const createTableSQL = """
      CREATE TABLE $tableName (
        id CHAR(32) PRIMARY KEY NOT NULL,
        avatar VARCHAR(120) NOT NULL,
        nickName VARCHAR(20) NOT NULL,
        note VARCHAR(20) NOT NULL,
        city VARCHAR(20) NOT NULL,
        lastMsgId CHAR(32) NOT NULL
      )""";

  static const columnSQL = "id, avatar, nickName, note, city, lastMsgId";

  static const insertSQL = """
  insert into $tableName ($columnSQL) values (?,?,?,?,?,?)
  """;

  static const queryAllSQL = """
  select $columnSQL from $tableName
  """;

  static const queryById = """
  select $columnSQL from $tableName where id = ? limit 1
  """;

  static const updateLastMsgSQL = """
  update $tableName set lastMsgId = ? where id = ?
  """;

  static const deleteSQL = """delete from $tableName""";

  String id;

  String nickName;

  String note;

  String city;

  String avatar;// 头像

  String lastMsgId; // 最后一条消息记录ID

  Friend(
      {this.id,
      this.nickName,
      this.note = "",
      this.city,
      this.avatar = "",this.lastMsgId = ""});

  static Friend fromJson(Map<String, dynamic> map) {
    return Friend(
      id: map["id"],
      nickName: map["nickName"],
      note: map["note"],
      city: map["city"],
      avatar: map["avatar"],
      lastMsgId: map["lastMsgId"]
    );
  }


  @override
  String toString() {
    return 'Friend{id: $id, nickName: $nickName, note: $note, city: $city, avatar: $avatar, lastMsgId: $lastMsgId}';
  }

  static List<Friend> fromJsonArray(List friendListJson) {
    return friendListJson.map((f) => fromJson(f)).toList();
  }

  @override
  String getSuspensionTag() {
    return convertToPinyin(this.note.isNotEmpty ? this.note : this.nickName);
  }
}
